package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.User;
import cn.itcast.service.AssessmentService;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    MemberService memberService;

    @RequestMapping("/add.do")
    public Result add(@RequestBody Map map){
        try {
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
            Member member = new Member();
            member.setRegTime(dateFormat.format(date));
            map.put("regTime",member.getRegTime());
            memberService.add(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    @RequestMapping("/deleteById.do")
    public Result deleteById(Integer id){
        try {
            memberService.deleteById(id);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    @RequestMapping("/findById.do")
    public Member findById(Integer id){
        return memberService.findById(id);
    }

    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    @RequestMapping("/delete.do")
    public Result delete(Integer id){
        try{
            memberService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }


    @RequestMapping("/findAll.do")
    public Result findAll(){
        try{
            List<Member> list = memberService.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findByPage.do")
    public PageResult findByPage(@RequestBody QueryPageBean pageBean) {

        // PageResult result = service.queryPage(pageBean);

        return memberService.queryPage(pageBean);
    }

    @RequestMapping("/getAddress.do")
    public List<User> getAddress(){
        return memberService.getAddress();
    }


    @RequestMapping("/download.do")
    public Result download(HttpServletResponse response) throws IOException {
        try {
            List<Map<String,Object>> list =memberService.findEvery();
            XSSFWorkbook excel = new XSSFWorkbook();
            XSSFSheet sheet = excel.createSheet("会员");
            XSSFRow title = sheet.createRow(0);

            title.createCell(0).setCellValue("id");
            title.createCell(1).setCellValue("档案号");
            title.createCell(2).setCellValue("姓名");
            title.createCell(3).setCellValue("性别");
            title.createCell(4).setCellValue("身份证");
            title.createCell(5).setCellValue("手机号");
            title.createCell(6).setCellValue("注册时间");
            title.createCell(7).setCellValue("密码(加密)");
            title.createCell(8).setCellValue("邮箱");
            title.createCell(9).setCellValue("生日");
            title.createCell(10).setCellValue("备注");
            title.createCell(11).setCellValue("年龄");
            title.createCell(12).setCellValue("民族");
            title.createCell(13).setCellValue("籍贯");
            title.createCell(14).setCellValue("婚姻状态");
            title.createCell(15).setCellValue("职业");
            title.createCell(16).setCellValue("文化程度");
            title.createCell(17).setCellValue("健康管理员");
            title.createCell(18).setCellValue("档案状态");
            title.createCell(19).setCellValue("身高");
            title.createCell(20).setCellValue("体重");
            title.createCell(21).setCellValue("舒张压");
            title.createCell(22).setCellValue("收缩压");
            title.createCell(23).setCellValue("呼吸");
            title.createCell(24).setCellValue("体温");
            title.createCell(25).setCellValue("腰围");
            title.createCell(26).setCellValue("臀围");
            title.createCell(27).setCellValue("BMI");
            title.createCell(28).setCellValue("血氧饱和度");
            title.createCell(29).setCellValue("既往史");
            title.createCell(30).setCellValue("家族史");
            title.createCell(31).setCellValue("过敏史");
            title.createCell(32).setCellValue("病史");
            title.createCell(33).setCellValue("慢性病");

            for (int i = 0; i < list.size(); i++) {
                XSSFRow data = sheet.createRow(i+1);
                Map<String, Object> map = list.get(i);

                if (map.get("id")!=null){
                    data.createCell(0).setCellValue(map.get("id").toString());
                }else {
                    data.createCell(0).setCellValue("未填写");
                }

                if (map.get("fileNumber")!=null){
                    data.createCell(1).setCellValue(map.get("fileNumber").toString());
                }else {
                    data.createCell(1).setCellValue("未填写");
                }

                if (map.get("name")!=null){
                    data.createCell(2).setCellValue(map.get("name").toString());
                }else {
                    data.createCell(2).setCellValue("未填写");
                }

                if (map.get("sex")!=null){
                    data.createCell(3).setCellValue(map.get("sex").toString());
                }else {
                    data.createCell(3).setCellValue("未填写");
                }

                if (map.get("idCard")!=null){
                    data.createCell(4).setCellValue(map.get("idCard").toString());
                }else {
                    data.createCell(4).setCellValue("未填写");
                }

                if (map.get("phoneNumber")!=null){
                    data.createCell(5).setCellValue(map.get("phoneNumber").toString());
                }else {
                    data.createCell(5).setCellValue("未填写");
                }

                if (map.get("regTime")!=null){
                    String regTime = new SimpleDateFormat("yyyy-MM-dd").format(map.get("regTime"));
                    data.createCell(6).setCellValue(regTime);
                }else {
                    data.createCell(6).setCellValue("未填写");
                }

                if (map.get("password")!=null){
                    data.createCell(7).setCellValue(map.get("password").toString());
                }else {
                    data.createCell(7).setCellValue("未填写");
                }

                if (map.get("email")!=null){
                    data.createCell(8).setCellValue(map.get("email").toString());
                }else {
                    data.createCell(8).setCellValue("未填写");
                }

                if(map.get("birthday")!=null){
                    String birthday = new SimpleDateFormat("yyyy-MM-dd").format(map.get("birthday"));
                    data.createCell(9).setCellValue(birthday);
                }else {
                    data.createCell(9).setCellValue("未填写");
                }

                if (map.get("remark")!=null){
                    data.createCell(10).setCellValue(map.get("remark").toString());
                }else {
                    data.createCell(10).setCellValue("未填写");
                }

                if (map.get("age")!=null){
                    data.createCell(11).setCellValue(map.get("age").toString());
                }else {
                    data.createCell(11).setCellValue("未填写");
                }

                if (map.get("nation")!=null){
                    data.createCell(12).setCellValue(map.get("nation").toString());
                }else {
                    data.createCell(12).setCellValue("未填写");
                }

                if (map.get("native_place")!=null){
                    data.createCell(13).setCellValue(map.get("native_place").toString());
                }else {
                    data.createCell(13).setCellValue("未填写");
                }

                if (map.get("marital_status")!=null){
                    data.createCell(14).setCellValue(map.get("marital_status").toString());
                }else {
                    data.createCell(14).setCellValue("未填写");
                }

                if (map.get("profession")!=null){
                    data.createCell(15).setCellValue(map.get("profession").toString());
                }else {
                    data.createCell(15).setCellValue("未填写");
                }

                if (map.get("culture")!=null){
                    data.createCell(16).setCellValue(map.get("culture").toString());
                }else {
                    data.createCell(16).setCellValue("未填写");
                }

                if (map.get("health_manager")!=null){
                    data.createCell(17).setCellValue(map.get("health_manager").toString());
                }else {
                    data.createCell(17).setCellValue("未填写");
                }

                if (map.get("file_status")!=null){
                    data.createCell(18).setCellValue(map.get("file_status").toString());
                }else {
                    data.createCell(18).setCellValue("未填写");
                }

                if (map.get("stature")!=null){
                    data.createCell(19).setCellValue(map.get("stature").toString());
                }else {
                    data.createCell(19).setCellValue("未填写");
                }

                if (map.get("weight")!=null){
                    data.createCell(20).setCellValue(map.get("weight").toString());
                }else {
                    data.createCell(20).setCellValue("未填写");
                }

                if (map.get("diastolic_blood_pressure")!=null){
                    data.createCell(21).setCellValue(map.get("diastolic_blood_pressure").toString());
                }else {
                    data.createCell(21).setCellValue("未填写");
                }

                if (map.get("systolic_pressure")!=null){
                    data.createCell(22).setCellValue(map.get("systolic_pressure").toString());
                }else {
                    data.createCell(22).setCellValue("未填写");
                }

                if (map.get("respiratory_frequency")!=null){
                    data.createCell(23).setCellValue(map.get("respiratory_frequency").toString());
                }else {
                    data.createCell(23).setCellValue("未填写");
                }

                if (map.get("animal_heat")!=null){
                    data.createCell(24).setCellValue(map.get("animal_heat").toString());
                }else {
                    data.createCell(24).setCellValue("未填写");
                }

                if (map.get("waistline")!=null){
                    data.createCell(25).setCellValue(map.get("waistline").toString());
                }else {
                    data.createCell(25).setCellValue("未填写");
                }

                if (map.get("hipline")!=null){
                    data.createCell(26).setCellValue(map.get("hipline").toString());
                }else {
                    data.createCell(26).setCellValue("未填写");
                }

                if (map.get("BMI")!=null){
                    data.createCell(27).setCellValue(map.get("BMI").toString());
                }else {
                    data.createCell(27).setCellValue("未填写");
                }

                if (map.get("oxyhemoglobin_saturation")!=null){
                    data.createCell(28).setCellValue(map.get("oxyhemoglobin_saturation").toString());
                }else {
                    data.createCell(28).setCellValue("未填写");
                }

                if (map.get("previous_history")!=null){
                    data.createCell(29).setCellValue(map.get("previous_history").toString());
                }else {
                    data.createCell(29).setCellValue("未填写");
                }

                if (map.get("family_history")!=null){
                    data.createCell(30).setCellValue(map.get("family_history").toString());
                }else {
                    data.createCell(30).setCellValue("未填写");
                }

                if (map.get("allergic_history")!=null){
                    data.createCell(31).setCellValue(map.get("allergic_history").toString());
                }else {
                    data.createCell(31).setCellValue("未填写");
                }

                if (map.get("medical_history")!=null){
                    data.createCell(32).setCellValue(map.get("medical_history").toString());
                }else {
                    data.createCell(32).setCellValue("未填写");
                }

                if (map.get("chronic_disease")!=null){
                    data.createCell(33).setCellValue(map.get("chronic_disease").toString());
                }else {
                    data.createCell(33).setCellValue("未填写");
                }

            }
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=member.xlsx" );
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
            excel.close();
            return null;
        } catch (IOException e) {
            return new Result(false,"下载失败");
        }
    }

    @Reference
    AssessmentService assessmentService;
    @RequestMapping("/findByMemberId.do")
    public Member findByMemberId(Integer id){
        Member mem = memberService.findById(id);
        mem.setAssessment(assessmentService.findByMemberId(id));
        return mem;
    }
}
